package com.atguigu.gulimall.order.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @Description
 * @Author lhj
 * @Date 2024/5/21 22:45
 */
@Slf4j
@Component
public class AlipayTradePagePay {

    public String tradePagePay(String orderSn, String totalAmount, String subject) {
        try {
            // 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());
            // 构造请求参数以调用接口
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            AlipayTradePagePayModel model = new AlipayTradePagePayModel();
            // 设置商户订单号
            model.setOutTradeNo(orderSn);
            // 设置订单总金额
            model.setTotalAmount(totalAmount);
            // 设置订单标题
            model.setSubject(subject);
            // 设置产品码
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            // 设置绝对超时时间
//            model.setTimeoutExpress("1m");
            request.setBizModel(model);
            // 支付成功后的同步返回
            request.setReturnUrl("http://member.gulimall.com/memberOrder.html");
            // 支付支付成功后的异步通知
            request.setNotifyUrl("http://jyniu6.natappfree.cc/payed/notify");
//            request.setReturnUrl("http://3c6et8.natappfree.cc/payed/notify");
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
            // 如果需要返回GET请求，请使用
            //AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
            String pageRedirectionData = response.getBody();
            log.info("tradePagePay result: {}", pageRedirectionData);
            if (response.isSuccess()) {
                log.info("aliPay request success");
            } else {
                log.error("aliPay request field");
                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                // System.out.println(diagnosisUrl);
            }
            return pageRedirectionData;
        } catch (AlipayApiException e) {
            log.error("aliPay field ", e);
            return null;
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCEoxxHmyFUNh9iHrhT0N5BBBpPJoOLsOYBJqThq42nF+YFUovWKjcmpZYlwT1WdRrngR9nJ3IfkS/nWfBXCmCi8hFGucCJUZMTil3XTs6RPQl/Xrle5g3Oj13sgWspvRqnoFYjytTDUhDcbOoMgwkEyhhJsmOzyHdPT2aDSuRELKJpQp7kc1X2vlSWOuSh50AIHpyP6a7Z8W+tduc1lfO9B1sW/84ylsjkHYNYvXgcBcfMHZkPITQWvv748XFSxxtar9x7eC8LG/hsBagnkRzqEk5QsRurlIUgx81x0TKZ387gbJIZluA9Nl1LtPrKmT61jmFcI9P8gMLqz273r3n7AgMBAAECggEAKXPYqMBg4DKyCDZVoUxLycIAfvAoeIXmvEFZknXNJYWgi9VKgrpa1ic2D8Ke6uBacjO2nsSDND5p3EINjQZfQU2p/JKdC82Ey7yYq2ujv1x2FWqDQJdhhUblMb8YwMirBexkP3dqm0iQoHMhipuBPA/1G0M3Sb7DGvRTs4C6aluYHHuFCjChtdeStxXOPyWB+Ua2HE3i5XLPtsbjIpKTcr5WlCaXCBwq1GFM9ZrhLqVVRmPp1A9KgrQYa2ZoBiOpNAjMv7Zyl0dyBTgDXW7+mxxxcuFM0PBUxvckLQdtp+v/MU8tVCUJR1EcqrhAI8+dWGgqrNIGr060+LT04Q0eMQKBgQD6XfgeaDadrOMFhAU+XGW0K0vs3cXhU45x9QrNzWfvHfHuvtoacWvpl6edbc+ipNlnTF6M7STjZlO5VqcetloNN+P4nT6EjoiQvrhs9iX9lNc9tTg32z84vNV8hXBpxuf7aF7/Ra9UUsHP37uIcbBKVbTK8Qv1Bm9RnJw0GsfZTwKBgQCHnw5jhEpeqpTIpqnfGqNnbmzamp4AOnKgAnuGGmQWZzeA3HhBG2THpOZ4asTAxgJbZyioyvSCQmWu+t8QvFOqeXaDfv6eMPUt39JCzJlK2jRiLsxpGQloy+MtplCaPJyyLk47+D8R+SyI39OdV6NfNrXHDR5gYcPljSt8YaRRlQKBgDDoHknCTbjrRsWTDnsqDWvucTxp7fegHNlB5sRg9YaOMO5UNcKomU3eJA3qn37D+PvmvSmGQYhyOrSfqrGjKWAfpNax0L6QITk1KE2Qn8czRgB13+95UpZAvOtAHb9r7XD86GGxORidueJjb/ytSKScrV5fS9inUyN8Fj9PY+6dAoGAQu2Vljr00J7x6cuFzVpCBYsTIRZonsg5RQm0l8tyVTP+yhMDLW4tX/Da49Yc3t1Y0G6qPBJcc+P8dkvLxWCLEXf7qo0DbmHOvSLViq2ZgL0KWOGrcrouUI89v4tx/4ul8LC1Xxb1WRNUjP+YWO+k6hn/vwmUUhAtMp1mNm2hP7kCgYA0yzF/Kp2d6ZmH5L5ekBjIT1e6d+aESUsRIxt80VAINARCdWCR8dNBn2nEl3q5ZqlAxP4PZkmKIZFf0pEecDa1/hhjRGsglitHwvgjDPVVEC/Ag7n4VyhIDaXnvc3PmlB8n8SwemnXzvgpGvrFGfNqZoUq1y8JMPccpdkKqOhcVw==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjb3+/tDaruUCkaLIxi75KxEooICkbdblafYCQFct2VHziutT8iZhXv2ZhW0beeizz3X+nnKXiv1ZTTQTREcwGBT7DCTRVe5GBy21KDCnFyYyFFQHstC4ErNJtkhV73vem4IKeVkfiQGw2TBqQsYjyMyFfsq7/rY0qe5DBskqtvfupJH99Z6rQmOPyc8O60+mjxdd70ZWic/kpeKqXCBroWENGcXVbWZcUvBAKi1gA2bMUvuLaOtEtpGzRzgC9Ox1HCXTo0+pooVahqHLb9TObL9CBJX50PAqUHB+19Qj1B66yTcYKQAG1NP720L82H9dtsiUkfHhmb9WuwUnL+AtrwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000137629127");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
